from collections import OrderedDict
import json
import codecs
import xlrd
import os
import sys
import shutil


def excelToJson(filePath, outPutPath):
    wb = xlrd.open_workbook(filePath)

    convert_list = []
    sh = wb.sheet_by_index(0)
    des = sh.row_values(0)
    print(des)
    idNum = len(des[0].split("|"))
    title = sh.row_values(1)
    valueType = sh.row_values(2)
    index = 2
    idMaps = {}
    for rownum in range(3, sh.nrows):
        rowvalue = sh.row_values(rownum)
        single = OrderedDict()
        cellId = ""
        for i in range(0, idNum):
            vType1 = valueType[i]
            value = rowvalue[i]
            if vType1 == "int":
                if cellId == "":
                    cellId = str(int(value))
                else:
                    cellId = cellId + "_" + str(int(value))
            elif vType1 == "float":
                if cellId == "":
                    cellId = str(int(value))
                else:
                    cellId = "_" + str(int(value))
            elif vType1 == "string":
                if cellId == "":
                    cellId = str(value)
                else:
                    cellId = "_" + str(value)

        idMaps[cellId] = index

        for colnum in range(0, len(rowvalue)):
            vType = valueType[colnum]
            if vType == "int":
                if rowvalue[colnum] == "":
                    single[title[colnum]] = 0
                else:
                    single[title[colnum]] = int(rowvalue[colnum])
            elif vType == "float":
                if rowvalue[colnum] == "":
                    single[title[colnum]] = 0
                else:
                    single[title[colnum]] = rowvalue[colnum]
            elif vType == "string":
                single[title[colnum]] = str(rowvalue[colnum])
        convert_list.append(single)
        index = index + 1

    endsingle = OrderedDict()
    for key in idMaps.keys():
        id = idMaps[key]
        endsingle[key] = id
    convert_list.insert(0, endsingle)

    lensingle = OrderedDict()
    lensingle["len"] = index - 2
    convert_list.insert(1, lensingle)
    j = json.dumps(convert_list)

    with codecs.open(outPutPath, "w", "utf-8") as f:
        f.write(j)


if __name__ == "__main__":

    curpath = os.path.dirname(os.path.abspath(sys.argv[0]))
    xlsxPath = curpath + "/xlsx"
    dirList = os.listdir(xlsxPath)

    for fileName in dirList:
        if os.path.isfile(xlsxPath + "/" + fileName):
            retIndex = fileName.find("~$")
            if retIndex == -1:
                realName = fileName.split(".")
                realName = realName[0]
                excelToJson(os.path.join(curpath + "/xlsx", fileName),
                            os.path.join(curpath + "/json", realName + ".json"))
                shutil.copy(os.path.join(curpath + "/json", realName + ".json"),
                            os.path.join(curpath + "/../GameServer/GameServer/config", realName + ".json"))
        else:
            pass

    os.system("pause")
